 //处理获取的原始数据
function data_input(data,list){
    let temp = data
    for (let i in temp){
      let x = temp[i]
      if(list.includes(x["id"])||list.length==0){
          add_data_in(x["id"],j=x["j"],w=x["w"],t=x["t"])
          //l_count = parseInt (x["id"])
      }
    }
}
 
 //获取所有值
function get_data(db,list) {
	  console.log("数据获取")
	  let transaction = db.transaction([storeName])	
	  let objectStore = transaction.objectStore(storeName)
    let request = objectStore.getAll()
	  
    request.onerror = function(event) {
        console.log('任务失败')
    }
 
    request.onsuccess = function(event) {
        let temp = request.result
        if(temp.length>0){
        data_input(temp,list)
        }
        else{
            add_data()
        }
    }
   }
  
function load_data(){
    get_search(db)
}
//新建数据库
function createindexDB() {
    //let dbName = 'helloIndexDB', version = 1, storeName = 'helloStore'

    let indexedDB = window.indexedDB
    //let db
    const request = indexedDB.open(dbName, version)
    request.onsuccess = function (event) {
        db = event.target.result // 数据库对象
        console.log('数据库打开成功...')
        load_data()
		}

    request.onerror = function (event) {
        console.log('数据库打开失败...')
    }

    request.onupgradeneeded = function (event) {
        // 数据库创建或升级的时候会触发
        console.log('onupgradeneeded')
        db = event.target.result
        let objectStore
        if (!db.objectStoreNames.contains(storeName)) {
            objectStore = db.createObjectStore(storeName, {
                keyPath: 'id'
            })} // 创建表1
        
        if (!db.objectStoreNames.contains(store_tag)) {
          objectStore = db.createObjectStore(store_tag, {
              keyPath: 'id'
          })}
          
        if (!db.objectStoreNames.contains(search_tag)) {
          objectStore = db.createObjectStore(search_tag, {
              keyPath: 're_list'
          })

          objectStore.add({"re_list":0,"search_tab":[]})
          objectStore.add({"re_list":1,"result_list":[]})
          console.log(objectStore)

        
        
        } // 创建表3
         
        }
}
    

//修改内容
function update(db, storeName, data) {
    let request = db.transaction([storeName], 'readwrite') // 事务对象
      .objectStore(storeName) // 仓库对象
      .put(data)
 
    request.onsuccess = function() {
      console.log('数据更新成功')
    }
 
    request.onerror = function() {
      console.log('数据更新失败')
    }
}

//增加条目
function addData(db, storeName, data) {
    let transaction = db.transaction([storeName],"readwrite")	
    let objectStore = transaction.objectStore(storeName)
    let request = objectStore.add(data)
    
    request.onerror = function(event) {
      if (request.error.name=="ConstraintError"){
        console.log('该内容已提交，将进行修改',request.error.name)
        update(db, storeName, data)
      }
      
    }

    request.onsuccess = function(event) {
      console.log('存储成功')
    }
	

}

//内容查询
function get(db, storeName, key) {
    let transaction = db.transaction([storeName]) // 事务
    let objectStore = transaction.objectStore(storeName) // 仓库对象
    let request = objectStore.get(key)

    request.onerror = function(event) {
      console.log('事务失败')
    }

    request.onsuccess = function(event) {
      console.log('主键查询结果: ', request.result)
    }
    }
 
 //清空
function del_all(db, storeName){	
    let transaction = db.transaction([storeName],"readwrite") // 事务
    let objectStore = transaction.objectStore(storeName) // 仓库对象
    objectStore.clear()
}
//删除部分值
function del_one(db,storeName,key){
		let transaction = db.transaction([storeName],"readwrite") // 事务
		let objectStore = transaction.objectStore(storeName) // 仓库对象
		objectStore.delete(parseInt(key))
		location.reload(false)
}